package com.ansion.basedb.dao.helper;


import com.ansion.basedb.storage.StorageSdk;
import com.ansion.basedb.storage.api.BaseKeyValueStorage;
import com.ansion.constant.BConstant;
import com.ansion.log.VLog;
import com.ansion.time.TimeUtils;
import com.ansion.utils.ContactUtil;
import com.ansion.utils.ServerTime;

public class FtsIndexHistoryHelper {
    private static final String KEY_CALLLOG_DIVIDER_ROW_ID = "divider_row_id";

    public static BaseKeyValueStorage getStorage() {
        return StorageSdk.getUserConfigStorage();
    }


    public static void onFirstSetTimeDivider() {
        if (!ContactUtil.isLoggedIn()) {
            VLog.i("TAG_SEARCH_FTS", "onAppStartOrLogin not login,return");
            return;
        }
        VLog.i("TAG_SEARCH_FTS", "onAppStartOrLogin");
        if (!getStorage().contains(BConstant.MIGRATE.KEY_FIRST_INSTALL_SERVER_TIME)) {//设置首次安装的serverTime
            long firstInstallServerTime = ServerTime.getServerTimeMillisecond();
            getStorage().putLong(BConstant.MIGRATE.KEY_FIRST_INSTALL_SERVER_TIME, firstInstallServerTime);
            VLog.i("TAG_SEARCH_FTS", "onAppColdStart save firstInstallServerTime %s", TimeUtils.getTimeString(firstInstallServerTime, TimeUtils.TIME_FORMAT_YMD_HMSS));
        }
        if (!getStorage().contains(BConstant.MIGRATE.KEY_FIRST_INSTALL_SYSTEM_TIME)) {
            long firstInstallSystemTime = System.currentTimeMillis();
            getStorage().putLong(BConstant.MIGRATE.KEY_FIRST_INSTALL_SYSTEM_TIME, firstInstallSystemTime);
            VLog.i("TAG_SEARCH_FTS", "onAppColdStart save firstInstallServerTime %s", TimeUtils.getTimeString(firstInstallSystemTime, TimeUtils.TIME_FORMAT_YMD_HMSS));
        }
        if (!getStorage().contains(BConstant.MIGRATE.KEY_FIRST_INSTALL_ROW_ID)) {
            long rowId = AppDatabaseHelper.getNextRowId();
            getStorage().putLong(BConstant.MIGRATE.KEY_FIRST_INSTALL_ROW_ID, rowId);
            VLog.i("TAG_SEARCH_FTS", "onAppColdStart save firstInstallRowId %s", rowId);
        }
    }

    public static void onSetDividerForCallLog() {
        if (!getStorage().contains(KEY_CALLLOG_DIVIDER_ROW_ID)) {//设置首次安装的serverTime
            long rowId = AppDatabaseHelper.getNextRowId();
            getStorage().putLong(KEY_CALLLOG_DIVIDER_ROW_ID, rowId);
            VLog.i("TAG_SEARCH_FTS", "onFirstSetCallLogDivider save divider row id %s", rowId);
        }
    }


    public static long getFirstInstallServerTime() {
        return getStorage().getLong(BConstant.MIGRATE.KEY_FIRST_INSTALL_SERVER_TIME, 0);
    }


    public static long getFirstInstallSystemTime() {
        return getStorage().getLong(BConstant.MIGRATE.KEY_FIRST_INSTALL_SYSTEM_TIME, 0);
    }

    public static long getFirstInstallRowId() {
        return getStorage().getLong(BConstant.MIGRATE.KEY_FIRST_INSTALL_ROW_ID, 0);
    }

    public static long getFirstInstallCallLogDividerRowId() {
        return getStorage().getLong(KEY_CALLLOG_DIVIDER_ROW_ID, 0);
    }


    public static void debugClearInstallTime() {
        getStorage().remove(BConstant.MIGRATE.KEY_FIRST_INSTALL_SERVER_TIME);
        getStorage().remove(BConstant.MIGRATE.KEY_FIRST_INSTALL_SYSTEM_TIME);
        getStorage().remove(BConstant.MIGRATE.KEY_FIRST_INSTALL_ROW_ID);
        getStorage().remove(KEY_CALLLOG_DIVIDER_ROW_ID);
    }
}
